package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.WcsLiteralDict;
import net.realsee.domain.wcs.data.bo.WcsLiteralDictBo;
import net.realsee.domain.wcs.data.vo.WcsLiteralDictVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.data.WcsLiteralDictMapper;
import net.realsee.service.wcs.data.IWcsLiteralDictService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 词典Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsLiteralDictServiceImpl extends ServiceImpl<WcsLiteralDictMapper, WcsLiteralDict> implements IWcsLiteralDictService {

    private final WcsLiteralDictMapper baseMapper;

    /**
     * 查询词典
     */
    @Override
    public WcsLiteralDictVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询词典列表
     */
    @Override
    public TableDataInfo<WcsLiteralDictVo> selectVoPage(WcsLiteralDictBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsLiteralDict> lqw = buildVoQueryWrapper(bo);
        Page<WcsLiteralDictVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询词典列表
     */
    @Override
    public List<WcsLiteralDictVo> selectVoList(WcsLiteralDictBo bo) {
        LambdaQueryWrapper<WcsLiteralDict> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsLiteralDict> buildVoQueryWrapper(WcsLiteralDictBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsLiteralDict> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsLiteralDict::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), WcsLiteralDict::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), WcsLiteralDict::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsLiteralDict::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsLiteralDict::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsLiteralDict::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsLiteralDict::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsLiteralDict::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsLiteralDict::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsLiteralDict::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsLiteralDict::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsLiteralDict::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsLiteralDict::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsLiteralDict::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsLiteralDict::getRowVersionStamp, bo.getRowVersionStamp());
        return lqw;
    }

    /**
     * 新增词典
     */
    @Override
    public Boolean insertByBo(WcsLiteralDictBo bo) {
        WcsLiteralDict add = BeanUtil.toBean(bo, WcsLiteralDict.class);
        return this.save(add);
    }

    /**
     * 修改词典
     */
    @Override
    public Boolean updateByBo(WcsLiteralDictBo bo) {
        WcsLiteralDict update = BeanUtil.toBean(bo, WcsLiteralDict.class);
        return this.updateById(update);
    }

    /**
     * 批量删除词典
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
